<template>
  <el-dialog width="500px" title="商户渠道配置" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
    <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
      <el-form-item prop="shopId" label="商户">
        <el-select v-model="dialogForm.shopId" filterable placeholder="请选择商户" style="width: 100%" clearable>
          <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
        </el-select>
      </el-form-item>
      <el-form-item prop="channelCode" label="渠道">
        <el-select v-model="dialogForm.channelCode" filterable placeholder="请选择渠道" style="width: 100%" clearable>
          <el-option v-for="item in channelOptions" :key="item.channelCode" :label="item.channelName" :value="item.channelCode" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitHandle" :loading="loading">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import api from '../api'
import { warningDeal, accDiv, successDeal } from '@/utils/utils'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      loading: false,
      dialogFormVisible: false,

      dialogForm: {
        isCostPrice: 0,
        isDiscountPrice: 0,
      },
      dialogFormRules: {
        shopId: [
          { required: true, message: '请选择商户', trigger: 'blur' },
        ],
        channelCode: [
          { required: true, message: '请选择渠道', trigger: 'blur' },
        ],
      },

      channelOptions: [],
      shopOptions: [],
      goodsForm: {},
    })

    function dialogShow() {
      getShopList()
      getChannelList()
      state.dialogFormVisible = true
    }

    function getChannelList() {
      api.channelList().then(res => {
        if (res.code == '200') {
          state.channelOptions = res.data?.list || []
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getShopList() {
      const obj = {}
      api.shopListAll(obj).then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (state.loading) return
          state.loading = true
          const obj = {
            shopId: state.dialogForm.shopId,
            channelCode: state.dialogForm.channelCode,
          }
          api.shopLimitBindChannelAdd(obj).then(res => {
            state.loading = false
            if (res.code == '200') {
              successDeal(res.msg)
              context.emit('dialogClose')
              state.dialogFormVisible = false
            } else {
              warningDeal(res.msg)
            }
          }).catch(err => {
            state.loading = false
            console.log(err)
          })
        }
      })
    }

    function dialogClose() {
      dialogFormRef.value && dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogShow,
      dialogClose,
      accDiv,
      submitHandle,
    }
  }
}
</script>

<style lang="scss" scoped>
.price-box {
  display: flex;
  box-sizing: border-box;
  padding-bottom: 20px;
  > div {
    flex-grow: 1;
  }
}
</style>